package com.yanqu.road.server.manger.activity.recycle;

import com.yanqu.road.dao.impl.activity.langyamemorial.LangYaMemorialUserDataDaoImpl;
import com.yanqu.road.entity.activity.ActivityItemRecycleData;
import com.yanqu.road.entity.activity.langyamemorial.LangYaMemorialLight;
import com.yanqu.road.entity.activity.langyamemorial.LangYaMemorialUserData;
import com.yanqu.road.entity.activity.langyamemorial.config.LangYaMemorialConfig;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.config.goods.OpenGoodsBagResult;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eItemId;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.player.UserBagItem;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.langyamemorial.LangYaMemorialMgr;
import com.yanqu.road.server.manger.config.GoodsBagMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

public class LangYaMemorialRecycleStrategy extends AbstractItemRecycleStrategy {

    @Override
    public void markNeedRecycleItem() {

        LangYaMemorialConfig config = LangYaMemorialMgr.getConfig();
        if(config != null){
            //赤焰砂
            List<Integer> paramList = StringUtils.stringToIntegerList(config.getRepairProp(), "=");
            int goodsItem = paramList.get(0);
            long recycleTime = config.getActivityInfo().getEndShowTime() * 1000L + 600 * 1000;
            //清空
            ActivityItemRecycleMgr.addRecycleItemData(goodsItem, activityType, 2, recycleTime, new ArrayList<>());
            //琅琊令也清空
            ActivityItemRecycleMgr.addRecycleItemData(eItemId.LANG_YA_LING.getValue(), activityType, 2, recycleTime, new ArrayList<>());


            //开宝箱
            List<Integer> itemList = StringUtils.stringToIntegerList(config.getAtticConsume(), "=");
            int atticItemId = itemList.get(0);
            List<String> params = new ArrayList<>();
            //参数1：待回收的道具
            params.add(String.valueOf(atticItemId));
            params.add(config.getActivityInfo().getActivityName());
            params.add(config.getLANGYA_ITEM_RETURN());
            ActivityItemRecycleMgr.addRecycleItemData(atticItemId, activityType, 1, recycleTime, params);

            //长明灯补发
            List<Integer> configList = StringUtils.stringToIntegerList(config.getKongmingReward(), "\\|");
            //长明灯宝箱
            int boxId = configList.get(1);
            int activityId = config.getActivityInfo().getActivityId();
            List<String> pList = new ArrayList<>();
            pList.add(activityId + "");
            pList.add(config.getActivityInfo().getActivityName());
            ActivityItemRecycleMgr.addRecycleItemData(boxId, activityType, 5, recycleTime, pList);
        }

    }

    @Override
    protected void initActivityType() {
        activityType = eActivityType.LangYaMemorial.getValue();
    }

    @Override
    protected void doRecycle(long userId, ActivityItemRecycleData data) {

        if (data.getRecycleType() == 2) {
            UserBagItem userBagItem = getUserBagItem(userId, data.getItemId());
            if (userBagItem != null && userBagItem.getNum() > 0) {
                removeItem(userBagItem);
            }
        }else if(data.getRecycleType() == 1){
            String language = UserMgr.getUserInfo(userId).getLanguage();
            UserBagItem userBagItem = getUserBagItem(userId, data.getItemId());
            if (userBagItem != null && userBagItem.getNum() > 0) {
                // 开宝箱
                GoodsInfo goodsInfo = GoodsMgr.getGoodsById(data.getItemId());
                if(goodsInfo != null){
                    long num = userBagItem.getNum();
                    if (removeItem(userBagItem)){
                        Property reward = PropertyHelper.parseStringToProperty(data.getParamList().get(2));
                        reward.rideProperty((int)num);
                        //  发邮件
                        String removeName = ServerLanguageMgr.getContent(goodsInfo.getGoodsName(), language);
                        String activityName = ServerLanguageMgr.getContent(data.getParamList().get(1), language);
                        String mailTitle = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.COMMON_RECYCLE_REMOVE_ITEM_TITLE, language), activityName);
                        String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.COMMON_RECYCLE_REMOVE_ITEM_CONTENT,
                                language), removeName, num);
                        MailManager.sendMail(userId, eMailType.SYSTEM.getValue(), PropertyHelper.parsePropertyToString(reward), mailContent, mailTitle);
                    }
                }
            }
        }else if(data.getRecycleType() == 5){
            //长明灯补发
            int box = data.getItemId();
            GoodsInfo goodsInfo = GoodsMgr.getGoodsById(box);
            int activityId = Integer.parseInt(data.getParamList().get(0));
            if(goodsInfo == null){
                log.info("LangYaMemorialRecycle activityId{}, box {} no exist", activityId, box);
            }
            LangYaMemorialUserData userData = new LangYaMemorialUserDataDaoImpl().getLangYaMemorialUserData(activityId, userId);
            if(userData != null){
                Property reward = new Property();
                int num = 0;
                for (LangYaMemorialLight light : userData.getLightList()) {
                    if(light.getGet() == 1){
                        continue;
                    }
                    num++;
                    light.setGet(1);
                    OpenGoodsBagResult openGoodsBagResult = GoodsBagMgr.getRandomGoods(box);
                    reward.addProperty(openGoodsBagResult.getReward());
                }
                if(num == 0){
                    return;
                }
                boolean result = new LangYaMemorialUserDataDaoImpl().update(userData);

                if(!result){
                    log.info("LangYaMemorialRecycle activityId{}, update user {} fail", activityId, userId);
                    return;
                }

                //邮件
                String language = UserMgr.getUserInfo(userId).getLanguage();
                GoodsInfo ingots = GoodsMgr.getGoodsById(GameConfig.GAME_MONEY_INGOTS);
                String scoreName = MultipleLanguageMgr.getContent(ingots.getGoodsName(), language);

                String removeName = ServerLanguageMgr.getContent(goodsInfo.getGoodsName(), language);
                String activityName = ServerLanguageMgr.getContent(data.getParamList().get(1), language);
                String mailTitle = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.ACTIVITY_GOODS_AUTO_CONSUME_TITLE, language), activityName);
                String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.ACTIVITY_GOODS_AUTO_CONSUME_CONTENT,
                        language), activityName, removeName, num, scoreName);
                MailManager.sendMail(userId, eMailType.SYSTEM.getValue(), PropertyHelper.parsePropertyToString(reward), mailContent, mailTitle);
            }
        }
    }

    @Override
    public boolean isActivityRunning() {
        return false;
    }
}
